//
//  IndividualVCHeaderEdit.m
//  quickLook
//
//  Created by J on 15/9/15.
//  Copyright (c) 2015年 JZJ. All rights reserved.
//

#import "IndividualVCHeaderEdit.h"
#import "Header.h"
#import "UIButton+WebCache.h"
@interface IndividualVCHeaderEdit ()<UIImagePickerControllerDelegate,UIActionSheetDelegate,UITextFieldDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (weak, nonatomic) IBOutlet UIBarButtonItem *leftBarbutton;
@property (weak, nonatomic) IBOutlet UIButton *headerIon;

@property (weak, nonatomic) IBOutlet UITextField *NickTextView;
@property (weak, nonatomic) IBOutlet UINavigationBar *titleView;
@end

@implementation IndividualVCHeaderEdit

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor whiteColor];
    // Do any additional setup after loading the view.
    _headerIon.layer.cornerRadius=30;
    _headerIon.clipsToBounds=YES;
    
    UIImage* image=[[UIImage imageNamed:@"backbuttonX.png"]TransformtoSize:CGSizeMake(20, 20)];
    [_leftBarbutton setImage:image];
    
    
    _titleView.titleTextAttributes=@{NSForegroundColorAttributeName:[UIColor grayColor]};

}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    NSUserDefaults* userdefault=[NSUserDefaults standardUserDefaults];

    NSDictionary* dic=[userdefault objectForKey:DATADIC];
    
    _NickTextView.text=dic[@"nickname"];
    if (!_headerIon.currentBackgroundImage) {
        [_headerIon sd_setBackgroundImageWithURL:[NSURL URLWithString:dic[@"avatar_url"]] forState:UIControlStateNormal];

    }
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)rightNaviButtonAction:(id)sender {
    
//http://api.kuaikanmanhua.com/v1/account/update
    
    NSUserDefaults* userdefault=[NSUserDefaults standardUserDefaults];
    
    
    
        [DownLoad changeNickNameandBlock:^(NSDictionary* obj, NSError *error) {
            
            
            JZJLog(@"%@",obj);
            if (obj[@"message"]) {
                
                [ProgressHUD showSuccess:@"修改成功" Interaction:YES];

                NSDictionary* dic=obj[@"data"];
                [userdefault setObject:dic forKey:DATADIC];
            
                [userdefault synchronize];
                
            }else if([obj[@"message"] isEqualToString:@"exist nickname"])
            
                [ProgressHUD showError:@"昵称已经存在啦" Interaction:YES];
            
            [NSTimer scheduledTimerWithTimeInterval:1.0f
                                                          target:self
                                                        selector:@selector(dismissAlertView:)
                                                        userInfo:nil
                                                         repeats:NO];
             
            
        }];
        
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
    
    

}


//自动消失
- (void)dismissAlertView:(NSTimer*)timer {
    [ProgressHUD dismiss];
}

- (IBAction)pop:(id)sender {
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)ChangeIconImage:(id)sender {
    
   
    
    //创建actionSheet
    
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从手机相册选择", nil];
    
    
    actionSheet.actionSheetStyle=UIActionSheetStyleBlackTranslucent;
    
    [actionSheet showInView:self.view];
    

    
}

#pragma mark --UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    JZJLog(@"buttonIndex:%d",buttonIndex);
    if (buttonIndex==1) {
        
        //调用相册
        
            UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
                pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                //pickerImage.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
                pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];
        
            }
            pickerImage.delegate = self;
            pickerImage.allowsEditing = NO;
            [self presentViewController:pickerImage animated:YES completion:nil];
        


    
    }else if(buttonIndex==0){
    
    
        
        
        //调用相机
        
        //先设定sourceType为相机，然后判断相机是否可用（ipod）没相机，不可用将sourceType设定为相片库
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
        //    if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
        //        sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //    }
        //sourceType = UIImagePickerControllerSourceTypeCamera; //照相机
        //sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //图片库
        //sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; //保存的相片
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化
        picker.delegate = self;
        picker.allowsEditing = YES;//设置可编辑
        picker.sourceType = sourceType;
        [self presentViewController:picker animated:YES completion:nil];//进入照相界面
        

        
    }
    
    
}

//点击相册中的图片 货照相机照完后点击use  后触发的方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    JZJLog(@"%@",info);
    [_headerIon setBackgroundImage:info[@"UIImagePickerControllerOriginalImage"] forState:UIControlStateNormal] ;

    
    
   }





@end
